package cn.com.ghy.android.widget.chat;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:客户详细信息仿EXCEL显示
 * @author wuz  @date 2011-5-13 09:40:52
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CustomerDetailInfoExcelView extends ListView {
	private Context context = null;
	private int rowHeight = 0;
	private String[] colTitles = null;
	private int[] colWidths = null;

	private ListView listView = null;
	private LinearLayout linearLayout = null;
	private HorizontalScrollView hsv = null;

	public CustomerDetailInfoExcelView(Context context, int rowHeight, String[] colTitles,
			int[] colWidths) {
		super(context);
		this.context = context;
		this.rowHeight = rowHeight;
		this.colTitles = colTitles;
		this.colWidths = colWidths;
		listView = new ListView(context);
	}

	public void setTitle() {

		listView.setHorizontalScrollBarEnabled(true);
		listView.setCacheColorHint(0x00000000);

		linearLayout = new LinearLayout(this.context);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams paras = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);

		LinearLayout layoutHeader = new LinearLayout(context);
		layoutHeader.setOrientation(LinearLayout.HORIZONTAL);
		layoutHeader.setBackgroundColor(Color.BLACK);

		for (int i = 0; i < colTitles.length; i++) {
			LinearLayout.LayoutParams layoutParaHeader = new LinearLayout.LayoutParams(
					colWidths[i], rowHeight);
			layoutParaHeader.setMargins(0, 0, 1, 1);

			TextView tvHeader = new TextView(context);
			tvHeader.setLines(1);
			tvHeader.setGravity(Gravity.CENTER);
			tvHeader.setBackgroundColor(Color.GRAY);
			tvHeader.setTextColor(Color.BLACK);
			tvHeader.setText(colTitles[i]);
			layoutHeader.addView(tvHeader, layoutParaHeader);
		}

		LinearLayout layoutHsv = new LinearLayout(context);
		layoutHsv.setOrientation(LinearLayout.VERTICAL);
		layoutHsv.addView(layoutHeader);
		layoutHsv.addView(listView);

		hsv = new HorizontalScrollView(context);
		hsv.addView(layoutHsv);
		linearLayout.addView(hsv, paras);
	}

	public ExcelAdapter createAdapter(List<String[]> list) {
		ExcelAdapter excelAdapter = new ExcelAdapter(context, list);
		listView.setAdapter(excelAdapter);
		return excelAdapter;
	}

	public View getView() {
		return linearLayout;
	}

	public class ExcelAdapter extends BaseAdapter {

		private Context context = null;
		private List<String[]> list = null;

		public ExcelAdapter(Context context, List<String[]> list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String[] data = list.get(position);
			return new ExcelRowView(context, data);
		}

		public void add(String[] data) {
			list.add(data);
			this.notifyDataSetChanged();
		}
	}

	private class ExcelRowView extends LinearLayout {

		private int colCount = 0;
		private View[] view = null;

		public ExcelRowView(Context context, String[] data) {
			super(context);
			colCount = data.length;
			view = new View[colCount];
			for (int i = 0; i < colCount; i++) {
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						colWidths[i], rowHeight);
				layoutParams.setMargins(0, 0, 1, 1);
				TextView tv = new TextView(context);
				tv.setLines(1);
				tv.setGravity(Gravity.LEFT);
				tv.setGravity(Gravity.CENTER_VERTICAL);
				tv.setTextSize(15);
				tv.setBackgroundColor(Color.WHITE);
				tv.setText(data[i]);
				this.addView(tv, layoutParams);
				view[i] = tv;
			}
			this.setBackgroundColor(Color.BLACK);
		}
	}
}
